#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/22 14:39
# @Author  : 王凯
# @File    : neimenggu_grade.py
# @Project : spider-man
import datetime

import scrapy

from apps.creadit_grade_a.creadit_grade_a.items import NetCreditGradeAItem
from utils.tools import parse_url_params


class NeimengguGradeSpider(scrapy.Spider):
    name = "neimenggu_grade"
    province = "内蒙古"
    url = "https://dzssjyy.neimenggu.chinatax.gov.cn:1717/Ajnsr/ajnsr.jsp"
    Request = scrapy.Request
    FormRequest = scrapy.FormRequest

    def start_requests(self):
        url = "https://dzssjyy.neimenggu.chinatax.gov.cn:1717/Ajnsr/ajnsr.jsp"
        yield self.Request(url, callback=self.parse_area)

    def parse_area(self, response, **kwargs):
        url = "https://dzssjyy.neimenggu.chinatax.gov.cn:1717/ajnsr/aJNsrdAtA.cmd"
        area_list = response.xpath('//*[@id="location_table"]//a/@onclick')
        year_list = [str(i) for i in range(2014, datetime.datetime.now().year)]
        for area in area_list:
            area_code = area.re_first(r"javascript:changetd\('(.*?)',this\);")
            for year in year_list:
                if str(year) in [str(i) for i in range(datetime.datetime.now().year - 1, datetime.datetime.now().year + 1)]:
                    data = {"page": "0", "location": area_code, "code": "", "name": "", "evalyear": str(year)}
                    yield self.FormRequest(url, formdata=data, method="POST", callback=self.parse_page)

    def parse_page(self, response, **kwargs):
        root_url, request_data = parse_url_params(response.request.body.decode())
        resp = response.json()
        total = resp.get("totalPages")
        url = "https://dzssjyy.neimenggu.chinatax.gov.cn:1717/ajnsr/aJNsrdAtA.cmd"
        yield from self.parse_detail(response, **kwargs)
        for page in range(1, int(total)):
            data = {
                "page": f"{page}",
                "location": request_data.get("location"),
                "code": "",
                "name": "",
                "evalyear": request_data.get("evalyear"),
            }
            yield self.FormRequest(url, formdata=data, method="POST", callback=self.parse_detail)

    def parse_detail(self, response, **kwargs):
        datas = response.json().get("content", [])
        for data in datas:
            item = NetCreditGradeAItem()
            item.taxpayer_id = data.get("code")
            item.company_name = data.get("name")
            item.year = data.get("evalyear")
            item.province = self.province
            yield item


def run():
    from scrapy import cmdline

    cmdline.execute("scrapy crawl neimenggu_grade".split())


if __name__ == "__main__":
    run()
